package business;

import entity.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class CalendarManager {
    
    @PersistenceContext
    private EntityManager entityManager;
    
    @EJB
    private UserManager userManager;

    public List<Calendar> findByUser(String username) {
        Query query = entityManager.createNamedQuery("Calendar.findByOwner");
        query.setParameter("owner", userManager.find(username));
        return query.getResultList();
    }

    public void save(Calendar calendar) {
        entityManager.merge(calendar);
    }

    public void create(Calendar calendar) {
        entityManager.persist(calendar);
    }

    public void delete(Calendar calendar) {
        entityManager.remove(calendar);
    }

    public boolean exists(int primaryKey) {
        return entityManager.find(Calendar.class, primaryKey) != null;
    }

    public Calendar find(int primaryKey) {
        return entityManager.find(Calendar.class, primaryKey);
    }
}
